﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sys_t_user_roleform.aspx.cs" Inherits="RTGS.Web.form.usermanagement.sys_t_user_roleform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server" id="Head1">
    <title></title>
    <link type="text/css" rel="stylesheet" href="<%= ResolveClientUrl("~/css/ui.jqgrid.css") %>" />
    
    <script type="text/javascript" src="<%= ResolveClientUrl("~/script/i18n/grid.locale-en.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveClientUrl("~/script/jquery.jqGrid.min.js") %>"></script>    

    <script src="<%= ResolveClientUrl("~/script/helper.js") %>" type="text/javascript"></script>
    <script type="text/javascript">

        $("#table").jqGrid({
            url: '<%= ResolveClientUrl("~/ajaxhandler/usermanagement/sys_t_user.ashx?method=getdata") %>',
            datatype: 'json',
            colNames: ['ID', 'select', 'User Name', 'Email'],
            colModel: [
           		{ name: 'userId', hidden: true, index: 'User_ID', width: 60 },
           		{ name: 'userId', index: 'User_ID', width: 100, formatter: radio },
           		{ name: 'userName', index: 'User_Name', width: 400 },
           		{ name: 'email', index: 'Email', width: 400 }
           	],
            rowNum: 10,
            id: 'User_ID',
            multiselect: false,
            rowList: [5, 10, 15, 20],
            pager: '#pager',
            sortname: 'User_ID',
            sortorder: "asc",
            viewrecords: true,
            jsonReader: { repeatitems: false },
            width: 600,
            height: '100%',
            caption: 'User',
            ajaxGridOptions: { cache: false },
            ondblClickRow: function () {
                //SelectUserID();
            }
        });

        function radio(value, options, rowObject) {
            var radioHtml = '<input type="radio" value=' + value + ' name="radio_'+value+'" />';
            return radioHtml;
        }

        function LoadRoleRightByUserId(userID) {
            parent.$('#lstRoleRight option').each(function (i, option) { $(option).remove(); });
            $listRight = parent.$('#lstRoleRight');
            $.ajax({
                type: 'POST',
                url: '<%= ResolveClientUrl("~/ajaxhandler/usermanagement/sys_t_user_role.ashx") %>?method=getdatabyuserid&userId=' + userID,
                contentType: 'application/json',
                dataType: 'json',
                success: function (datas) {
                    $.each(datas, function (i, data) {
                        $listRight.append('<option value="' + data.roleId + '">' + data.roleName + '</option>');

                    });

                },
                error: function () {
                    alert('Failed to load user role by selected user');
                }
            });
        }

        function LoadRoleLeftByUserId(userID) {

            parent.$('#lstRoleLeft option').each(function (i, option) { $(option).remove(); });
            $listLeft = parent.$('#lstRoleLeft');

            $.ajax({
                type: 'POST',
                url: '<%= ResolveClientUrl("~/ajaxhandler/usermanagement/sys_t_user_role.ashx") %>?method=getdatabyuserid&userId=' + userID,
                contentType: 'application/json',
                dataType: 'json',
                success: function (datas) {
                    $.each(datas, function (i, data) {

                        $listLeft.append('<option value="' + data.roleId + '">' + data.roleId + '</option>');

                    });

                },
                error: function () {
                    alert("Failed to load available role");
                }
            });

        }

        $('#btnOk').click(function () {

            var grid = $("#table");
            var ids = grid.jqGrid('getDataIDs');
            for (var i = 0; i <= ids.length - 1; i++) {
                var userId = grid.jqGrid('getCell', ids[i], 'userId');
                var userName = grid.jqGrid('getCell', ids[i], 'userName');
                var chDelete = $('#table' + ' input[name=radio_' + userId + ']').prop('checked');
                if (chDelete) {
                    parent.$("input[name^='txtUserCode']").val(userName);
                    parent.$("input[name^='txtUserID']").val(userId);
                    LoadRoleRightByUserId(userId);
                    parent.$('#dialog').dialog('close');
                }
            }
        });
    
    </script>

    <style type="text/css">
        #btnOk
        {
            width: 64px;
        }
    </style>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    <br />
    <table id="table" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pager" class="scroll">
    </div>
    <br />
    <div id="dialog" title="Default dialog">
    </div>
     <input id="btnOk" type="button" value="Ok" />
    </form>
     
</body>
</html>
